package com.kaifamiao.jdbc.core;

import java.sql.Connection;
import java.sql.DriverManager;

public class DriverLoadAndRegister {

    public static void main(String[] args) throws Exception {

        /*
        final String driverClassName = "oracle.jdbc.OracleDriver";
        final String url = "jdbc:oracle:thin:@localhost:1521:kaifamiao";
        final String user = "kaifamiao";
        final String password = "kaifamiao";
        */

        final String driverClassName = "com.mysql.cj.jdbc.Driver";
        final String url = "jdbc:mysql://localhost:3306/kaifamiao";
        final String user = "kfm";
        final String password = "kfm";

        // 1、注册驱动
        // 通过反射技术加载指定类名的类，并将其初始化
        // Driver接口的实现类会在 类初始化阶段 创建实例 并 向DriverManager注册
        // DriverManager 会尽可能管理向它注册过的驱动
        Class.forName( driverClassName ); // ClassNotFoundException

        // 2、获得连接
        Connection conn = DriverManager.getConnection( url, user, password ); // SQLException
        System.out.println( conn );
        System.out.println( conn.isClosed() );
        System.out.println( conn.isValid( 0 ) );
        System.out.println( conn.isReadOnly() );

        conn.close();

    }

}
